//
//  HDRecognizeeCell.swift
//  HuaDaoPeifu
//
//  Created by ZERO on 2018/6/7.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

enum HDRecognizeeCellMode {
    case Selectable,NoSelectable
}

import UIKit

@objc protocol HDRecognizeeCellDelegate{
    
    func recognizeeCellWillDelete(cell:HDRecognizeeCell)
    func recognizeeCellWillModify(cell:HDRecognizeeCell)
    func recognizeeCellWillSelect(cell:HDRecognizeeCell)
}

class HDRecognizeeCell: UITableViewCell {

    var delegate: HDRecognizeeCellDelegate?
    @IBOutlet weak var IdentifierLabel: UILabel!
    
    @IBOutlet weak var isCheckLabel: UILabel!
    
    @IBOutlet weak var relationLabel: UILabel!
    
    @IBOutlet weak var modifyBtn: UIButton!
    
    @IBOutlet weak var deleteBtn: UIButton!
    
    @IBOutlet weak var selectView: UIView!
    
    @IBOutlet weak var selectBtn: UIButton!
    
    
    @IBOutlet weak var deleteBtnWidthConstraint: NSLayoutConstraint!
    
    
    var navi: UINavigationController!
    
    var row : NSInteger!
    
    var canSelected: Bool = false{
        didSet{
            selectBtn.isHidden = !canSelected
            deleteBtnWidthConstraint = deleteBtnWidthConstraint.setMultiplier(multiplier: canSelected ? 0.33 : 0.5)
        }
    }
    
    var recognizee : HDRecognizeeModel!{
        didSet{
            
            IdentifierLabel.text = recognizee.partone?.name
            isCheckLabel.isHidden = (recognizee.identify == 0)
            switch recognizee.relation {
            case "0":
                relationLabel.text = "本人"
                break
            case "1":
                relationLabel.text = "配偶"
                break
            case "2":
                relationLabel.text = "子女"
                break
            default:
                relationLabel.text = "其他"
                break
            }
        }
    }
    
    var mode : HDRecognizeeCellMode = .NoSelectable{
        didSet{
            switch mode {
            case .Selectable:
                selectView.isHidden = false
                break
            case .NoSelectable:
                selectView.isHidden = true
                break
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        selectBtn.isHidden = true
        deleteBtnWidthConstraint = deleteBtnWidthConstraint.setMultiplier(multiplier: 0.5)
    }
  
    // 修改
    @IBAction func modifyAction(_ sender: UIButton) {

        if let delegate = delegate {
            delegate.recognizeeCellWillModify(cell: self)
        }
        
    }
    
    // 删除
    @IBAction func deleteAction(_ sender: UIButton) {
        if let delegate = delegate {
            delegate.recognizeeCellWillDelete(cell: self)
        }
    }
    
    // 选择
    @IBAction func selectAction(_ sender: UIButton) {
        selectBtn.isSelected = !selectBtn.isSelected
        if let delegate = delegate {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
                delegate.recognizeeCellWillSelect(cell: self)
            }
        }
    }
    
}
